package com.authority.basic.general_basic.controller;


import com.authority.basic.general_basic.entiy.MyCustomEvent;
import com.authority.basic.general_basic.entiy.User;
import com.authority.basic.general_basic.service.IOrderService;
import com.authority.basic.general_basic.service.IUserService;
import com.authority.basic.general_basic.test_thread.myThread;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@RestController
@RequestMapping("user")
@Api(tags = "UserController", value = "")
public class UserController
{
    @Autowired
    ApplicationContext context;

    @Autowired
    private IUserService userService;

    @Autowired
    Map<String, IOrderService> orderServiceMap =new HashMap<>();

    @PostMapping("/addUser")
    @ApiOperation("用户保存")
    @ApiImplicitParam(name ="user",value = "用户实体",dataType = "user")
    public void addUser(@RequestBody @Valid User user) {
        // 如果有参数校验失败，会将错误信息封装成对象组装在BindingResult里
        for(int i=1;i<100000;i++){
            User user1=new User();
            user1.setId(user.getId()+i);
            user1.setName(user.getName()+i);
            userService.save(user1);
        }


    }


    @GetMapping("/getUser")
    public String getUser() {
       /* User user = new User();
        user.setId(1L);
        user.setAccount("12345678");
        user.setPassword("12345678");
        user.setEmail("123@qq.com");*/
        /*QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("id",Long.valueOf("1087982257332887553"));*/
        System.out.println(orderServiceMap.size());
        User user = new User();
        user.setId(1L);
        user.setEmail("123@qq.com");
        context.publishEvent(new MyCustomEvent(user,"发布了新事件"));
        return  orderServiceMap.get("A").say();


        //return userService.getOne(queryWrapper);
    }



    @PostMapping("/updateUser")
    @ApiOperation("用户并发保存")
    @ApiImplicitParam(name ="user",value = "用户实体",dataType = "user")
    public void updateUser() throws InterruptedException {
        // 如果有参数校验失败，会将错误信息封装成对象组装在BindingResult里
        Runnable run1=new Runnable() {
            public void run() {
                User user1=new User();
                user1.setId(Long.parseLong("111112"));
                user1.setName("王二");
                userService.updateById(user1);
            }
        };
        Runnable run2=new Runnable() {
            public void run() {
                User user=new User();
                user.setId(Long.parseLong("111111"));
                user.setName("李四");
                userService.updateById(user);
            }
        };
        List<Runnable> list=new ArrayList<Runnable>();
        list.add(run1);
        list.add(run2);
        //concurrencyUpdate(list);
        final CountDownLatch start=new CountDownLatch(1);//计数器 设1
        final CountDownLatch end=new CountDownLatch(list.size());

        for ( Runnable run:list) {
            myThread thread = new myThread(run, start, end);
            thread.start();
        }
        long startTime = System.nanoTime();
        System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
        start.countDown();//计数器减一，两个线程同时执行
        end.await();
        System.out.println("****end*****");
    }

   /* public void concurrencyUpdate( List<Runnable> task)throws Exception{


    }*/



}
